package com.LGF.Book.action;

import com.LGF.Book.bean.Book;
import com.LGF.Book.bean.BookType;
import com.LGF.Book.biz.BookBiz;
import com.LGF.Book.biz.BookTypeBiz;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/bookType.let")
public class BookTypeServlet extends HttpServlet {

    //创建BookTypeBiz对象
    BookTypeBiz bookTypeBiz = new BookTypeBiz();
    //创建BookBiz对象
    BookBiz bookBiz = new BookBiz();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /**
     * /bookType.let?type=query 图书类型信息表
     * /bookType.let?type=add 添加图书类型
     * /bookType.let?type=modifypre&id=xx 修改图书类型前准备
     * /bookType.let?type=modify 修改图书类型
     * /bookType.let?type=remove 删除图书类型
     * /bookType.let?type=selectBookType 根据图书名模糊查询图书类型信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //创建输出流对象,打印信息
        PrintWriter out = resp.getWriter();

        //创建session对象
        HttpSession session = req.getSession();
        String type = req.getParameter("type");

        //根据类型判断请求接口
        switch (type){
            case "query":
                query(req,resp,out);
                break;
            case"add":
                add(req,resp,out);
                break;
            case"modifypre":
                modifypre(req,resp,out);
                break;
            case"modify":
                modify(req,resp,out);
                break;
            case"remove":
                remove(req,resp,out);
                break;
            case"selectBookType":
                //bookType.let?type=selectBookType
                selectBookType(req,resp,out);
                break;

        }

    }

    /**
     * 实现根据图书类型名模糊查询图书类型信息
     * @param req
     * @param resp
     * @param out
     */
    private void selectBookType(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
        //接收前端传过来的search值
        String search = req.getParameter("search");

        //调用biz层方法模糊查询获取图书类型信息
        List<BookType> bookTypeList = bookTypeBiz.selectTName(search);

        //判断是否获取成功
        if (bookTypeList!=null){
            //转成json数据
            JSONObject json = new JSONObject();
            json.put("data",bookTypeList);
            json.put("code",0);
            json.put("count",bookTypeList.size());

            //打印
            out.print(json);

        }
        else {
            out.println("<script>alert('前端传递信息有误');location.href='booType_list.jsp';</script>");
        }




    }

    /**
     * 实现删除图书类型功能
     * @param req
     * @param resp
     * @param out
     */
    private void remove(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
        //接收前端传过来的id值
        long id = Long.parseLong(req.getParameter("id"));
        //根据类型id判断该类型是否添加过图书
        Book book = bookBiz.getBytypeId(id);
        if (book==null){
            //说明该类型没有添加过图书

            //调用biz层根据id删除图书类型信息
            int count = bookTypeBiz.remove(id);
            //判断是否删除成功
            if(count >0){
                out.println("<script>alert('图书类型删除成功');location.href='bookType_list.jsp';</script>");

            }
            else{
                out.println("<script>alert('图书类型删除失败');location.href='bookType_list.jsp';</script>");

            }


        }
        else{
            //说明该类型添加过图书 不能删除该类型
            out.println("<script>alert('该类型添加过图书不能删除');location.href='bookType_list.jsp';</script>");
        }


    }

    /**
     * 实现修改图书类型功能
     * @param req
     * @param resp
     * @param out
     */
    private void modify(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
        //接收前端传过来的id和tname值
        long id = Long.parseLong(req.getParameter("id"));
        String tname = req.getParameter("tname");

        //调用biz层修改图书信息
        int count = bookTypeBiz.modify(id, tname);
        //判断是否修改成功
        if (count>0){
            out.println("<script>alert('图书类型修改成功');location.href='bookType_list.jsp';</script>");
        }
        else {
            out.println("<script>alert('图书类型修改失败');location.href='booType_list.jsp';</script>");
        }

    }

    /**
     * 修改图书类型前准备
     * @param req
     * @param resp
     * @param out
     */
    private void modifypre(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) throws ServletException, IOException {
        //接收前端传过来的id值
        long bookTypeId = Long.parseLong(req.getParameter("id"));
        //调用biz层方法获取图书类型信息
        BookType bookType = bookTypeBiz.getById(bookTypeId);
        if (bookType!=null){
            //存储session对象
            req.setAttribute("bookType",bookType);

            req.getRequestDispatcher("bookType_modify.jsp").forward(req,resp);

        } else{
            out.println("<script>alert('信息有误');location.href='admin_Main.jsp';</script>");
        }






    }

    /**
     * 实现添加图书类型功能
     * @param req
     * @param resp
     * @param out
     */
    private void add(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
        //接收前端传过来的tname值
        String tname = req.getParameter("tname");

        //根据传过来的图书类型名称 判断类型是否存在
        //调用biz 层 根据图书名称获取图书类型信息
        BookType bookType = bookTypeBiz.getByTname(tname);
        if (bookType==null){
            //说明没有这个图书类型名称
            //调用biz层添加图书add
            int count = bookTypeBiz.add(tname);
            //判断是否添加成功
            if(count>0){
                out.println("<script>alert('图书类型添加成功');location.href='bookType_list.jsp';</script>");
            }
            else{
                out.println("<script>alert('图书类型添加失败');location.href='bookType_list.jsp';</script>");

            }

        }
        else{
            //说明图书类型名称已存在
            out.println("<script>alert('图书类型已存在');location.href='bookType_add.jsp';</script>");
        }



    }

    /**
     * 实现查询所有图书信息 提供分页
     * @param req
     * @param resp
     * @param out
     */
    private void query(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
        //调用biz层获取所有图书类型信息
        List<BookType> all = bookTypeBiz.getAll();

        //调用前端传过来的 page 和 limit
        int limit = Integer.parseInt(req.getParameter("limit"));
        int page = Integer.parseInt(req.getParameter("page"));
        int pageIndex = (page-1)*limit;
        //调用biz层getByPage
        List<BookType> bookTypeList = bookTypeBiz.getByPage(pageIndex, limit);

        //转成json数据
        JSONObject json = new JSONObject();
        json.put("data",bookTypeList);
        json.put("code",0);
        json.put("count",all.size());

        //打印
        out.print(json);



    }
}
